#include "DarkGDK.h"
#include "SC_Collision.h"

void DarkGDK ( void )
{
	dbSetDisplayMode( 800,600,32 );

	dbMakeObjectCylinder( 1,10 );
	dbColorObject( 1,dbRGB(200,0,0) );

	dbAutoCamOff( );
	dbPositionCamera( 0,0,-30 );

	SC_Start( );

	//setup the object for collision
	SC_SetupObject( 1,0,0 );

	//make a bullet marker
	dbMakeObjectPlain( 501,2,2 );
	dbColorObject( 501,dbRGB(255,255,255) );
	dbHideObject( 501 );

	//make a white image, since coloring doesn't work very well with no light
	//makeColorImage(1,1,rgb(255,255,255))

	//texture object 501,1
	dbSetObjectLight( 501,0 );

	int rotatetimer = dbTimer( );
	int collide = 0;

	while ( LoopGDK( ) )
	{
		dbYRotateCamera( dbCameraAngleY() + ( dbMouseMoveX()/10.0f ) );
		dbXRotateCamera( dbCameraAngleX() + ( dbMouseMoveY()/10.0f ) );
		dbPositionMouse( 400,300 );
		
		//spacebar for rotation
		if ( dbSpaceKey() == 1 && rotatetimer < dbTimer() )
		{
			rotatetimer = dbTimer() + 300;
			
			dbRotateObject( 1,(float)dbRnd(359),(float)dbRnd(359),(float)dbRnd(359) );
			
			//updates the object, including scaling if allowed.
			SC_UpdateObject( 1 );
		}
		
		//hide the bullet marker
		dbHideObject( 501 );
		
		if ( dbMouseClick() == 1 )
		{			
			//get our collision vector
			float oldx = dbCameraPositionX();
			float oldy = dbCameraPositionY();
			float oldz = dbCameraPositionZ();
			dbMoveCamera( 200 );
			float x = dbCameraPositionX();
			float y = dbCameraPositionY();
			float z = dbCameraPositionZ();
			dbMoveCamera( -200 );
			
			collide = SC_RayCast( 0, oldx,oldy,oldz, x,y,z, 0 );
			
			if ( collide > 0 )
			{
				//get the collision point			
				float newx = SC_GetStaticCollisionX();
				float newy = SC_GetStaticCollisionY();
				float newz = SC_GetStaticCollisionZ();
				//get collision normal
				float normx = SC_GetCollisionNormalX();
				float normy = SC_GetCollisionNormalY();
				float normz = SC_GetCollisionNormalZ();
				
				//position and point a marker in the right direction
				dbPositionObject( 501, newx + normx/100.0f, newy + normy/100.0f, newz + normz/100.0f );
				dbPointObject( 501, newx + normx, newy + normy, newz + normz );
				dbShowObject( 501 );
			}
		}
		
		dbText( 0,0,"Move mouse to aim, left click to shoot" );
		dbText( 0,20,"Press space to randomly rotate object" );
		dbText( 0,40,"Press Esc to quit" );
		dbText( 0,80,dbStr(dbScreenFPS()) );
		dbText( 0,100,dbStr(collide) );
		
		dbInk( dbRgb(255,0,0),0 );
		dbCircle( 400,300,10 );
		dbDot( 400,300 );
		dbInk( dbRgb(255,255,255),0 );
		
		dbSync( );
	}
}